<?php
class DatesController extends AppController {
	
	var $name = 'Dates' ;
	var $components = array ( 'RequestHandler' ) ;
	var $helpers = array ( 'Html' , 'Text' , 'Time' , 'Ajax' , 'chat/AjaxChat' , 'context' ) ;
	var $paginate = array ( 'limit' => 4 , 'page' => 1 , 'order' => array ( 'title' => 'asc' ) ) ;
	
	function display1 ( $id = 1 ) {
		
		if (! $this->RequestHandler->isAjax ()) {
			// things you want to do on initial page load go here			$this->pageTitle = "Date List" ;
		}
		
		$this->Date->id = $id ;
		$this->set ( 'id', $id ) ;
		$this->set ( 'Dates', $this->Date->findAll ( array ( 'is_genre' => $this->data [ 'Search' ] [ 'is_genre' ] , 'search_genre' => $this->data [ 'Search' ] [ 'search_genre' ] ) ) ) ;
		$this->set ( 'Dates', $this->paginate ( 'Date' ) ) ;
		
		if ($this->RequestHandler->isAjax ()) {
			$this->viewPath = 'elements' . DS . 'dates' ;
			$this->render ( 'paging' ) ;
		}
		lg ( $id, 1 ) ;
		if ($id)
			switch ( $id) {
				case 'add' :
					$this->addDate () ;
				break ;
				case 'delDate' :
					$this->delDate () ;
				break ;
			} else {
		
		}
	}
	
	/**
	 * Dates
	 */
	
	function display ( $id = 1 ) {
		if (! $this->RequestHandler->isAjax ()) {
			$this->pageTitle = "Date List" ;
			// clear the session on first page visit			$this->Session->del ( $this->name . '.search' ) ;
		}
		
		$this->Date->id = $id ;
		$this->set ( 'id', $id ) ;
		
		if ($id)
			switch ( $id) {
				case 'add' :
					$this->addDate () ;
				break ;
				case 'delDate' :
					$this->delDate () ;
				break ;
			}
		
		if (! empty ( $this->data ))
			$search = $this->data [ 'search' ] ; elseif ($this->Session->check ( $this->name . '.search' ))
			$search = $this->Session->read ( $this->name . '.search' ) ;
		
		$filters = array ( ) ;
		lg ( @$search ) ;
		
		if (isset ( $search )) {
			$filters = array ( 'is_genre' => $search [ 'is_genre' ] , 'search_genre' => $search [ 'search_genre' ] ) ;
			//			"lower(Date.text) like '%".low($search['text'])."%'",			$this->Session->write ( $this->name . '.search', $search ) ;
		}
		//		$this->set('Dates',$this->Date->findAll(array()));		$this->set ( 'Dates', $this->paginate ( 'Date', $filters ) ) ;
		
		if ($this->RequestHandler->isAjax ()) {
			$this->viewPath = 'elements' . DS . 'dates' ;
			$this->render ( 'paging' ) ;
		}
	}
	
	/**
	 * Add date
	 *
	 */
	function add () {
		if (empty ( $this->data )) {
			$this->render () ;
		} else {
			$this->cleanUpFields () ;
			//			lg($this->data,1);			if ($this->Date->save ( $this->data )) {
				$this->Session->setFlash ( 'The Date Category has been saved' ) ;
				$this->redirect ( '/dates/display/1' ) ;
			} else {
				$this->Session->setFlash ( 'Please correct errors below.' ) ;
			}
		}
	
	}
	
	/**
	 * delete Date
	 *
	 */
	function delDate () {
		
		if (empty ( $this->data )) {
			$this->render () ;
		} else {
			$this->cleanUpFields () ;
			lg ( $this->Date ) ;
			if ($this->Date->save ( $this->data )) {
				$this->Session->setFlash ( 'The Date Category has been saved' ) ;
				$this->redirect ( '/dates/display/1' ) ;
			} else {
				$this->Session->setFlash ( 'Please correct errors below.' ) ;
			}
		}
	
	}
	
	function drop () {
		$this->pageTitle = "Date drag" ;
		$this->layout = "default" ;
		$this->RequestHandler->isAjax () ;
		
		$this->set ( 'dates', $this->Date->findAll () ) ; /* no articles bought yet */
	}
	
	function showcart () {
		$this->layout = "default" ;
		/* this function can be used to show initial contents of the cart. */
		//
		$this->set ( 'dates', false ) ; /* no articles bought yet */
	/* (read on to find out more about this view) */
	}
	
	function adddate () {
		/* The DOM id of the element that has been dragged into here can be found in $this->params['form']['draggedid'].
		 you can use that id to do something like $this->Article->findById() to add the article to the session.
		 At the end of this function the entire contents of the <strong>view</strong> file will be returned (and rendered inside the
		 <div> with DOM id 'cart'.)  The layout won't be rendered, only the view, since this is an Ajax call (the difference is autodetected by Cakes RequestHandler)
		 You could do something like this: */
		
		@$this->params[ 'form' ][ 'draggedid' ] ;
		
		$all_bought_articles_from_the_session = $this->Date->findAll();
		
		
		$this->set ( 'dates', $all_bought_articles_from_the_session);
		
		$this->render ( 'cart' ) ;
	}
	function removedate () {
		/* this function is similar to the above but instead removes one article from the session and renders the same view ;-) */
	}
}
?>